/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.POJO_GheNgoi;
import POJO.POJO_ChuyenTau;
import POJO.POJO_VeTau;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author USER
 */
public class DAO_VeTau {
    DataProvider data;
    
    public List<POJO_VeTau> LoadList()throws SQLException {
        data = new DataProvider();
        data.Open();
        
        String query = "SELECT * FROM ve_tau";
        
        List<POJO_VeTau> lst = new ArrayList<>();
        ResultSet rs = data.ExecuteQuery(query);
        
        while (rs.next()) {
            POJO_VeTau vt = new POJO_VeTau();
            vt.setMAVE(rs.getString("MaVe"));
            vt.setMAGHE(rs.getString("MaGhe"));
            vt.setMACHUYEN(rs.getString("MaChuyen"));
            vt.setGIAVE(rs.getInt("GiaVe"));
            lst.add(vt);
        }
        
        return lst;
    } 
    
    
    public int xoaVeTau(POJO_VeTau vt) {
        
            data = new DataProvider();
            data.Open();
            
            String sql = String.format("DELETE FROM `ve_tau` WHERE MaVe = '%s'", vt.getMAVE());
            
            return data.ExeCuteUpdate(sql);
    }
    
    public int themVeTau(POJO_VeTau vt) {
        
            data = new DataProvider();
            data.Open();
            
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String sql = String.format("INSERT INTO `ve_tau`(`MaVe`, `MaGhe`, `MaChuyen`, `GiaVe`) VALUES ('%s','%s','%s',%d)",
                    vt.getMAVE(),
                    vt.getMAGHE(),
                    vt.getMACHUYEN(),
                    vt.getGIAVE()
            );                               
            return data.ExeCuteUpdate(sql);
    } 
    
    public int capNhatVeTau(POJO_VeTau vt)  {
        
            data = new DataProvider();
            data.Open();    
        
            String sql = String.format("UPDATE `ve_tau` SET `MaGhe`='%s',`MaChuyen`='%s',`GiaVe`= %d WHERE MaVe = '%s'",                    
                    vt.getMAGHE(),
                    vt.getMACHUYEN(),
                    vt.getGIAVE(),
                    vt.getMAVE()
            );
                    
            return data.ExeCuteUpdate(sql);
    }
}
